// 异步操作
import { Menu } from "@/api";
import { Role } from "@/api";

// 加载菜单列表 
let menu_list = async (context) => {
    let { status, msg, data } = await Menu.all({ type: 'flat' });
    if (status) {
        context.commit('MENU_LIST', data);
    }
    return { status, msg };
}

// 添加菜单
let add_menu = async (context, playload) => {
    let { status, msg, data } = await Menu.add(playload);
    if (status) {
        context.commit('ADD_MENU', { ...data, ...playload });
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 添加操作按钮
let add_operation = async (context, playload) => {
    let { status, msg, data } = await Menu.add_operation(playload);
    if (status) {
        context.commit('ADD_OPERATION', { ...data, ...playload });
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 编辑菜单
let update_menu = async (context, payload) => {
    let { menu_id } = payload;
    let { status, msg } = await Menu.update(menu_id, payload);
    if (status) {
        context.commit('UPDATE_MENU', payload);
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 编辑操作按钮
let update_operate = async (context, payload) => {
    let { permission_id } = payload;
    let { status, msg } = await Menu.edit_operation(permission_id, payload);
    if (status) {
        context.commit('UPDATE_OPERATE', payload);
    }
    // 尽量在组件中实现跳转。弹窗等功能；return出去的是Promise对象
    return { status, msg }
}

// 删除菜单
let remove_menu = async (context, payload) => {
    let { menu_id, permission_id } = payload;
    let res = context.state.menu.some(item => item.parent_id === permission_id);
    if (!res) {
        let { status, msg } = await Menu.remove_menu(menu_id, { permission_id });
        if (status) {
            // 通过传的id找索引
            context.commit('REMOVE_MENU', permission_id);
        }
        return { status, msg }
    } else {
        alert("存在子级路由!");
    }
}

// 删除操作按钮
let remove_operation = async (context, payload) => {
    let { permission_id } = payload;
    let res = context.state.menu.some(item => item.parent_id === permission_id);
    if (!res) {
        let { status, msg } = await Menu.remove_operation(permission_id);
        if (status) {
            // 通过传的id找索引
            context.commit('REMOVE_OPERATION', permission_id);
        }
        return { status, msg }
    } else {
        alert("存在子级路由!");
    }
}

// 根据角色id，获取角色的菜单、操作权限
let menu_power = async (context, payload) => {
    let { status, msg, data } = await Role.menu_power({ role_id: payload, type: 'flat' });
    let selectedMenu = [];
    data.forEach(function (item) {
        selectedMenu.push(item.permission_id);
    });
    if (status) {
        // 通过传的id找索引
        context.commit('MENU_POWER', selectedMenu);
    }
    return { status, msg, selectedMenu };
}

//  配置角色--菜单权限
let set_menu_power = async (context, payload) => {
    let { status, msg, data } = await Role.set_menu_power(payload);
    // 需要调用mutation吗？
    return { status, msg, data };
}

export default {
    menu_list,
    add_menu,
    update_menu,
    add_operation,
    update_operate,
    remove_menu,
    remove_operation,
    menu_power,
    set_menu_power,
}